package com.sharding.demo.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * The type Mybatis generator.
 */
public class MybatisGenerator {

	/**
	 * 
	 * 数据库地址 如：jdbc:mysql://116.7.249.36:3306/user
	 */
	private String DB_URL;
	/**
	 * 数据库用户名 如：root
	 */
	private String USER_NAME;
	/**
	 * 数据库密码 如：123456
	 */
	private String PASSWORD;
	/**
	 * 数据库驱动
	 */
	private String DRIVER = "com.mysql.cj.jdbc.Driver";
	/**
	 * 生成包代码根路径 如：com.wt.oboa
	 */
	private String PACKAGE;
	/**
	 * 生成代码类的用户签名 如：cwli
	 */
	private String AUTHOR;
	/**
	 * 生成代码的输出路径必须全路径 如：D:\xxx
	 */
	private String OUTPUT_FILE_PATH;

	/**
	 * Instantiates a new Mybatis generator.
	 * @param dB_URL the d b url
	 * @param uSER_NAME the u ser name
	 * @param pASSWORD the p assword
	 * @param pACKAGE the p ackage
	 * @param aUTHOR the a uthor
	 * @param oUTPUT_FILE_PATH the o utput file path
	 */
	public MybatisGenerator(String dB_URL, String uSER_NAME, String pASSWORD, String pACKAGE, String aUTHOR,
			String oUTPUT_FILE_PATH) {
		super();
		DB_URL = dB_URL + "?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=UTC";
		USER_NAME = uSER_NAME;
		PASSWORD = pASSWORD;
		PACKAGE = pACKAGE;
		AUTHOR = aUTHOR;
		OUTPUT_FILE_PATH = oUTPUT_FILE_PATH;
	}

	/**
	 * 不生成controller 自动生成映射代码 使用说明：new一个本类对象使用main方法即可运行
	 * @param tableNames the table names
	 */
	public void generateByTables(String... tableNames) {
		GlobalConfig config = new GlobalConfig();

        DataSourceConfig dataSourceConfig = new DataSourceConfig();
		dataSourceConfig.setDbType(DbType.MYSQL).setUrl(DB_URL).setUsername(USER_NAME).setPassword(PASSWORD)
				.setDriverName(DRIVER);
		StrategyConfig strategyConfig = new StrategyConfig();
		strategyConfig.setCapitalMode(true).setEntityLombokModel(true).setRestControllerStyle(true)
				.setNaming(NamingStrategy.underline_to_camel).setInclude(tableNames);
		config.setActiveRecord(false)
				//.setSwagger2(true)
				.setBaseResultMap(true).setBaseColumnList(true)
				.setEnableCache(true).setAuthor(AUTHOR).setOutputDir(OUTPUT_FILE_PATH).setFileOverride(false);
		config.setServiceName("I%sService").setOpen(false);
		new AutoGenerator().setGlobalConfig(config).setDataSource(dataSourceConfig).setStrategy(strategyConfig)
				.setPackageInfo(new PackageConfig().setParent(PACKAGE).setEntity("entity")).execute();
	}
}
